//
//  UIViewController+share.m
//  XPhoto
//
//  Created by baboy on 2019/10/22.
//  Copyright © 2019 baboy. All rights reserved.
//

#import "UIViewController+share.h"
#import "PostAction.h"
#import <UShareUI/UShareUI.h>
@implementation UIViewController(share)


- (void)shareWebPageToPlatformType:(UMSocialPlatformType)platformType withPost:(Post *)post{
    NSString *thumbUrl = [post.items.firstObject thumb]?:[post.items.firstObject url];
    //创建分享消息对象
    UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];
    //创建网页内容对象
    UMShareWebpageObject *shareObject = [UMShareWebpageObject shareObjectWithTitle:post.user.name
                                                                             descr:post.content
                                                                         thumImage:thumbUrl];
    //设置网页地址
    shareObject.webpageUrl =@"http://mobile.umeng.com/social";
    //分享消息对象设置分享内容对象
    messageObject.shareObject = shareObject;
    //调用分享接口
    [[UMSocialManager defaultManager] shareToPlatform:platformType messageObject:messageObject currentViewController:self completion:^(id data, NSError *error) {
        if (error) {
            NSLog(@"************Share fail with error %@*********",error);
        }else{
            NSLog(@"response data is %@",data);
        }
    }];
}

- (void)shareWithPost:(Post *)post{
    PostAction *action = [PostAction new];
    action.postId = post.id;
    
    //显示分享面板
    [UMSocialUIManager showShareMenuViewInWindowWithPlatformSelectionBlock:^(UMSocialPlatformType platformType, NSDictionary *userInfo) {
        // 根据获取的platformType确定所选平台进行下一步操作
        DLOG(@"%@", userInfo);
        [self shareWebPageToPlatformType:platformType withPost:post];
    }];
    
    [PostAction addShare:action
                callback:^(id  _Nonnull task, id  _Nullable json, NSError * _Nullable error) {
    }];
}
@end
